<?php
// $Id: shop.php 207 2012-03-14 09:52:26Z tomqin@gmail.com $

class Shop_Controller extends FrontController{

	var $res_id;
	var $menu_type;

	function init(){
		$this->res_id = intval($this->action);

		$this->zone = App::loadModule('zone', true);
		$this->zone->initCity();
	}

	function _onDefault(){
		$store  = $this->getStore();
		if($store){
			//$dishcat = $this->getDishCat();
			$dishes  = $this->getDishes($dishcat);

			$store['url'] = App::frontUrl('shop', $store['res_id']);
			$this->setVar('encryptId', App::crypt($store['res_id']));
			$this->setVar('store', $store);
			$this->setVar('dishcat', $dishcat);
			$this->setVar('dishes', $dishes);
		}

		$bigzone  = $this->zone->getZoneById($store['zone_id'])->name;
		$subzone  = $this->zone->getZoneById($store['subzone_id'])->name;
		$seo_name = $store['name'];
		$seo_zone = $bigzone.$subzone;

		$title   = "{$seo_name}外卖网_外卖电话_外卖信息_{$seo_zone}快餐外卖送餐_网上订餐";

		$this->seoTitle($title);
		$this->setVar('bigzone', $bigzone);
		$this->setVar('subzone', $subzone);
		$this->setVar('mode', $this->param ? $this->param : 'list');

		$this->render('shop-index');
	}

	private function getStore(){
		return DB::getOne(DB::getSql('*', 't_store', array('res_id' => $this->res_id)));
	}

//	private function getDishCat(){
//		return DB::getAll(DB::getSql('*', 't_dish_cat', array('res_id' => $this->res_id), null, 'ordnum ASC'), null, 'dish_cat_id');
//	}

	private function getDishes(&$dishcats){
		$dishcats = DB::getAll(DB::getSql('*', 't_dish_cat', array('res_id' => $this->res_id), null, 'ordnum ASC'), null, 'dish_cat_id');

		$dishes   = DB::getAll(DB::getSql('*', 't_dish', array('res_id' => $this->res_id), null, 'dish_id ASC'));
		foreach($dishes as $dish){
			$cat_id = $dish['dish_cat_id'];
			$cat_id = isset($dishcats[$cat_id]) ? $cat_id : 0;

			$data[$cat_id][$dish['dish_id']] = $dish;
		}
		if(isset($data[0])){
			$tmp = $data[0];
			unset($data[0]);
			$data[0] = $tmp;
		}

		return $data;
	}
}
?>